package com.cfc.stage1;

import common.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 后续遍历  左右根
 *   https://leetcode.cn/problems/binary-tree-postorder-traversal/
 * @author: yangxuan10
 * @create: 2024-04-24 09:38
 **/
public class PostorderTraversal {

    @Test
    public void test1() {
        System.out.println(postorderTraversal(TreeNode.generateTree(1)));
        System.out.println(postorderTraversal(TreeNode.generateTree(2)));
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        return postorderTraversal(list, root);
    }

    public List<Integer> postorderTraversal(List<Integer> list, TreeNode root) {
        if (root.left != null) {
            postorderTraversal(list, root.left);
        }
        if (root.right != null) {
            postorderTraversal(list, root.right);
        }
        list.add(root.val);
        return list;
    }

}